package com.bst.web.base;

import com.bst.base.domain.BaseWeather;
import com.bst.base.service.IBaseWeatherService;
import com.bst.common.base.service.IBaseDWService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.page.TableDataInfo;
import com.bst.common.utils.DateUtil;
import com.bst.common.utils.DateUtils;
import com.bst.common.vo.DateParamVO;
import com.bst.md.service.IMdMedDimDataService;
import com.bst.md.service.IMdMedDimService;
import com.bst.md.vo.MdMedDimParamVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 维度管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-07
 */
@Controller
@RequestMapping("/base/md/dim/date")
public class MdMedDimDateController extends BaseController
{

    private static final String DIM_TABLE_DAY = "md_dim_day";

    @Autowired
    IMdMedDimService mdMedDimService;
    @Autowired
    IBaseDWService baseDWService;
    @Autowired
    IBaseWeatherService baseWeatherService;
    @Autowired
    IMdMedDimDataService mdMedDimDataService;

    @GetMapping("/day")
    public String weather()
    {
        return "system/md/med/dimTb/day";
    }

    @PostMapping("/queryDayByMonth")
    @ResponseBody
    public TableDataInfo queryDayByMonth(String month) {
        List<Map<String,Object>> list = mdMedDimDataService.queryDayDataByMonth(month);
        return getDataTable(list);
    }

    @PostMapping("/data/withWhere")
    @ResponseBody
    public TableDataInfo queryCommonDataWithWhere(MdMedDimParamVO paramVO) {
        List<Map<String,Object>> list = mdMedDimDataService.queryCommonDataWithWhere(paramVO);
        return getDataTable(list);
    }

    @ApiOperation(value="插入天数据")
    @RequestMapping(value="insertDay", method= RequestMethod.GET)
    @ResponseBody
    public AjaxResult insertDay(String startDate,String endDate) {
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            Date dtBegin = DateUtil.FORMAT_YYYY_MM_DD_HHMMSS.parse(startDate+" 00:00:00");
            Date dtEnd = DateUtil.FORMAT_YYYY_MM_DD_HHMMSS.parse(endDate+" 00:00:00"),dtNext;
            String dBegin;int counterStop=0;
            Map<String,Object> dayMap;
            DateParamVO dateParamVO = new DateParamVO();
            dateParamVO.setDayBegin(startDate);
            dateParamVO.setDayEnd(endDate);
            List<BaseWeather> weathers = baseWeatherService.selectBaseWeatherListByDays(dateParamVO);
            Map<String,BaseWeather> weatherMap = weathers.stream().collect(Collectors.toMap(BaseWeather::getDayOccur, e->e));
            BaseWeather weather;
            while (dtBegin.before(dtEnd) && counterStop++ < 10000) {
                if(counterStop%200==0 && list.size()>0) {
                    baseDWService.insertMapList("md_dim_day",list.get(0),list);
                    list = new ArrayList<>();
                }
                dBegin = DateUtil.toDateStrByFormat(dtBegin, "yyyy-MM-dd");
                weather = weatherMap.get(dBegin);
                dayMap = new TreeMap<>();
                dayMap.put("id_day",dBegin);
                dayMap.put("mm",dBegin.substring(0,7));
                dayMap.put("yy",dBegin.substring(0,4));
                dayMap.put("qq",dBegin.substring(0,4)+(Integer.parseInt(dBegin.substring(5,7))+2)/3);
                dayMap.put("ww",DateUtil.getWeek(dtBegin)+"");
                dayMap.put("ww_no",DateUtils.getYearWeekNo(DateUtil.FORMAT_YYYY_MM_DD.parse(dBegin)));
                dayMap.put("eu_cls", DateUtils.judgeHoliday(dBegin));
                if(weather!=null) {
                    dayMap.put("tmp_mx",weather.getTempMax());
                    dayMap.put("tmp_mi",weather.getTempMin());
                }
                list.add(dayMap);
                dtNext = DateUtil.getNextDate(dtBegin, 3);
                dtBegin = dtNext;
            }
            if(list.size()>0) {
                baseDWService.insertMapList("md_dim_day",list.get(0),list);
            }
            return AjaxResult.success();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    
}
